﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBlog.Models;

namespace MvcBlog.Controllers
{
    /// <summary> Main Blog Page Content </summary>
    public class BlogController : Controller
    {
        BlogDbContext db = new BlogDbContext();
        PostsRepository posts = new PostsRepository();
        //
        // GET: /Blog/

        /// <summary> Show latest posts </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View(posts.GetLatestPosts());
        }

        
        /// <summary> Show post by category </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public ActionResult Posts(int category)
        {
            return View("Index", posts.GetPosts(category));
        }
        

        /// <summary> Show post by month </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public ActionResult Arhive(int year, int month)
        {
            return View("Index", posts.GetPosts(year, month));
        }


        /// <summary> Show selected post </summary>
        /// <param name="id"> post id </param>
        /// <returns></returns>        
        public ActionResult Details(int id)
        {
            var post = db.Posts.Find(id);
            return View(post); //View(posts.GetPost(id));
        }

        /// <summary> Show Category List in Sidebar </summary>
        /// <returns></returns>
        public ActionResult PostCategories()
        {
            return PartialView(posts.GetPostCategories());
        }

        /// <summary> Show Arhives List as partial in sidebar </summary>
        /// <returns></returns>
        public ActionResult Archives()
        {
            return PartialView(posts.GetArhives());
        }

        public ActionResult SearchResults(string SearchString)
        {
            return View(posts.GetPosts(SearchString));
        }

        protected override void Dispose(bool disposing)
        {
            posts.Dispose();
            base.Dispose(disposing);
        }

    }
}
